﻿// -------------------------------------------------------------------------
//    @FileName         :    NFWorldWelfareMgr.h
//    @Author           :    gaoyi
//    @Date             :    2025-06-30
//    @Email			:    445267987@qq.com
//    @Module           :    NFWorldWelfareMgr
//
// -------------------------------------------------------------------------

#pragma once

#include "NFComm/NFObjCommon/NFObject.h"
#include "NFLogicCommon/NFLogicShmTypeDefines.h"
#include "proto_welfare.nanopb.h"
#include "proto_welfare.pb.h"
#include "NFLogicCommon/NFWelfareConf.h"

class NFWorldWelfareMgr : public NF_OBJECT_GLOBAL_TEMPLATE_MACRO(NFWorldWelfareMgr, EOT_WORLD_WELFARE_MGR_ID, NFObject)
public:
    NFWorldWelfareMgr();
    ~NFWorldWelfareMgr() override;
    int CreateInit();
    int ResumeInit();
    int OnTimer(int timeId, int callCount) override;
    int Awake() override;
    int OnHandleServerMessage(uint64_t unLinkId, NFDataPackage& packet) override;
public:
    const NFWelfareConf& GetConf() const { return m_stConf; }
    int GetWorldDataOpenIDs(std::set<uint64_t>& stSetOpenId) const;
public:
    int Tick();
    int OnHandleSyncWelfareData(uint64_t unLinkId, NFDataPackage& packet);
public:
    int ProcConfChange(bool& bConfChange);
    int LoadWelfareXls(NFWelfareConf& stConf, uint32_t dwGameOpenDayBeginUnixSec);
    int CheckAndSetDBConfig(const NFWelfareConf& stConf, WelfareDBWorldDataConfig& stWelfareDBWorldDataConfig, bool& bChange);
    int CheckAndSetDBDeleteWelfareConfig(WelfareDBWorldDataConfig& stWelfareDBWorldDataConfig, bool& bChange);
    int CheckAndUpdDBConfigWelfare(WelfareDBWorldDataConfigWelfare& stDBConfig, const NFWelfareConfOne& stWelfareConfOne, bool& bChange);
    int CheckAndUpdDBConfigWelfareNode(WelfareDBWorldDataConfigWelfareNode& stDBConfig, const E_WelfareNode& stConf, bool& bChange);
    int FillDBConfigWelfare(const NFWelfareConfOne stWelfareConfOne, WelfareDBWorldDataConfigWelfare& stDBConfig);
    int CheckConfHadLoad() const;
public:
    // 计算福利开启关闭
    int ProcOpenClose(uint32_t dwUnixSec, uint32_t dwLastTickUnixSecPre, bool bConfChange);
    // 检测是否 要展示
    int CheckForShow(const E_Welfare* pDesc, uint32_t dwUnixSec);
    // 检测是否 不再展示
    int CheckForHide(const E_Welfare* pDesc, uint32_t dwUnixSec);
    // 检测是否 关闭
    int CheckForClose(const E_Welfare* pDesc, uint32_t dwUnixSec);
    // 检测是否 开启
    int CheckForOpen(const E_Welfare* pDesc, uint32_t dwUnixSec);

    int CheckIsOpenAndInTimeRange(const E_Welfare* pDesc, const UnixTimeRange& timeRange, uint32_t dwUnixSec, bool& bOpenAndInTimeRange) const;
    int CheckIsShowBySvrOpenTime(const E_Welfare* pDesc) const;
public:
    int SyncAllConfToGameSvr(int iSvrID, int iSyncType) const;
    int SyncShowAndOpenToGameSvr(int iSvrID, int iSyncType) const;

    int GetAllConf(proto_ff::SSWorldWelfareSyncConfRes& res) const;
    int GetShowAndOpen(proto_ff::SSWorldWelfareSyncOpenShowRes& res) const;
public:
    static bool CanDeleteWelfareDesc(const WelfareDBWorldDataConfigWelfare& stDBConfig);

public:
    uint32_t GetConfLastChangeUnixSec() const { return m_dwConfLastChangeUnixSec; }
private:
    bool IsOpenWelfareId(uint64_t welfareId);
    void EraseOpenWelfareId(uint64_t welfareId);
    int AddOpenWelfareId(uint64_t welfareId);
    bool IsShowWelfareId(uint64_t welfareId);
    void EraseShowWelfareId(uint64_t welfareId);
    int AddShowWelfareId(uint64_t welfareId);
private:
    static_assert(WELFARE_DEFINE_SHOW_NUM_MAX >= WELFARE_DEFINE_OPEN_NUM_MAX, "");
    WelfareOpenShowWorldData m_stWorldOpenShowData;

    uint32_t m_dwConfLastChangeUnixSec; // 最后检测到配置变动的时刻
    uint32_t m_dwLastTickUnixSec; // 上次tick的时刻

    // 最后配置变更时的开服时刻
    // 在tick时和当前开服时刻比对，发现变动时，触发配置重载计算用
    uint32_t m_dwGameOpenUnixSec;

    NFWelfareConf m_stConf;
    int32_t m_iOldConfigNum;
};
